package server

import (
	"net/http"

	"github.com/google/uuid"
	"github.com/labstack/echo/v4"
	"gitverse.ru/pahtll/denisovich/internal/models"
)

func (s *Server) CreateReviewHandler(c echo.Context) error {
	review := c.Get("review").(*models.Review)

	err := s.repository.CreateReview(c.Request().Context(), review)
	if err != nil {
		return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
	}

	return c.JSON(http.StatusOK, review.Id)
}

func (s *Server) DeleteReviewHandler(c echo.Context) error {
	uuidString := c.Param("id")
	id := uuid.MustParse(uuidString)

	err := s.repository.DeleteReview(c.Request().Context(), id)
	if err != nil {
		return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
	}
	return c.JSON(http.StatusOK, id)
}

func (s *Server) GetReviewHandler(c echo.Context) error {
	uuidString := c.Param("id")
	id := uuid.MustParse(uuidString)

	review, err := s.repository.GetReviewById(c.Request().Context(), id)
	if err != nil {
		return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
	}
	return c.JSON(http.StatusOK, review)
}

func (s *Server) GetReviewsHandler(c echo.Context) error {
	reviews, err := s.repository.GetAllReviews(c.Request().Context())
	if err != nil {
		return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
	}
	return c.JSON(http.StatusOK, reviews)
}

func (s *Server) GetReviewBySenderNameHandler(c echo.Context) error {
	senderName := c.Param("sender_name")

	reviews, err := s.repository.GetReviewsBySenderName(c.Request().Context(), senderName)
	if err != nil {
		return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
	}
	return c.JSON(http.StatusOK, reviews)

}
